﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using System.IO.IsolatedStorage;

namespace CarBloke
{
    public class Vehicle
    {
        public Guid ID { get; set; }

        public String Name { get; set; }

        public int Year { get; set; }

        public String Notes { get; set; }

        public String MilesSinceLastOilChange
        {
            get
            {
                String distanceString = LanguageResources.Resources._lastOilChange + ": " + LanguageResources.Resources._unknown;
                VehicleStats stats = App.LoadVehicleStats(this.ID);

                if (stats != null)
                {
                    double distanceKm = stats.DistanceSinceOilChange;

                    if (!Double.IsNaN(distanceKm) && !Double.IsInfinity(distanceKm))
                    {
                        double distance = UnitConversions.Convert(distanceKm, ApplicationSettings.DistanceUnits.Kilometers, ApplicationSettings.Current.DistanceUnit);
                        int distanceInt = (int)Math.Round(distance);
                        distanceString = LanguageResources.Resources._lastOilChange + ": " + distanceInt.ToString() + " " + ApplicationSettings.ToLongString(ApplicationSettings.Current.DistanceUnit, true);
                    }
                }                               

                return distanceString;
            }
        }

        public String MostRecentFuelEfficiency
        {
            get
            {
                String efficiencyString = String.Empty;
                VehicleStats stats = App.LoadVehicleStats(this.ID);

                if (stats != null)
                {
                    double efficiency = stats.MostRecentFuelEfficiency;
                    if (!Double.IsNaN(efficiency) && !Double.IsInfinity(efficiency))
                    {
                        efficiency = UnitConversions.Efficiency(efficiency, ApplicationSettings.Current.EfficiencyUnit);
                        int roundedEfficiency = (int)Math.Round(efficiency);
                        efficiencyString = roundedEfficiency.ToString() + " " + ApplicationSettings.ToString(ApplicationSettings.Current.EfficiencyUnit);
                    }
                }
                
                return efficiencyString;
            }
        }

        public DateTime MostRecentFuelEfficiencyDate
        {
            get
            {
                DateTime mostRecent = DateTime.MinValue;
                VehicleStats stats = App.LoadVehicleStats(this.ID);

                if (stats != null)
                {
                    mostRecent = stats.MostRecentFuelEfficiencyDate;
                }

                return mostRecent;
            }
        }
    }
}
